"""
消息模型
"""
from sqlalchemy import Column, String, Boolean, DateTime, Enum as SQLEnum, JSON
from sqlalchemy.sql import func
from app.core.database import Base
import enum


class MessageType(str, enum.Enum):
    """消息类型"""
    BOOKING = "booking"  # 预约消息
    TEAM = "team"  # 球队消息
    TEAM_APPLICATION = "team_application"  # 入队申请
    TEAM_APPLICATION_RESULT = "team_application_result"  # 申请结果
    PAYMENT = "payment"  # 支付消息
    SYSTEM = "system"  # 系统消息


class Message(Base):
    """消息表"""
    __tablename__ = "messages"
    
    id = Column(String(36), primary_key=True, index=True)
    user_id = Column(String(36), nullable=False, index=True)
    type = Column(SQLEnum(MessageType), nullable=False)
    title = Column(String(200), nullable=False)
    content = Column(String(1000), nullable=False)
    is_read = Column(Boolean, default=False, index=True)
    link = Column(String(500))  # 跳转链接
    extra_data = Column(JSON)  # 额外数据（JSON格式）
    
    # 时间戳
    created_at = Column(DateTime(timezone=True), server_default=func.now())
    
    def to_dict(self):
        """转换为字典"""
        return {
            "id": self.id,
            "type": self.type,
            "title": self.title,
            "content": self.content,
            "isRead": self.is_read,
            "createdAt": self.created_at.isoformat() if self.created_at else None,
            "link": self.link,
            "extraData": self.extra_data
        }

